/* model */
import { 
  BaseSelectUserStoreSelectUserListDataType,
  BaseSelectUserVuexPropertyEnum 
} from '@src/component/common/BaseSelectUser/store'
import { BaseSelectUserSearchMoreTypeEnum } from '@src/component/common/BaseSelectUser/model'
import MsgModel from '@model/MsgModel'
/* store */
import { useBaseSelectUserStore } from '@src/component/common/BaseSelectUser/store/hooks/base'
/* vue */
import { computed } from 'vue'
/* type */
import { 
  BaseSelectUserGetSearchMoreUserListModel,
  BaseSelectUserGetSearchMoreUserListResult,
  BaseSelectUserGetSearchUserListModel, 
  BaseSelectUserGetSearchUserListResult
} from '@src/component/common/BaseSelectUser/types'
/* util */
import { isEmpty, isNotEmpty } from '@src/util/type'

/** 
 * @description 选人控件 搜索人员的列表数据
*/
function useBaseSelectUserStoreSearchUserListData() {
  
  const store = useBaseSelectUserStore()
  
  // 搜索人员的列表数据
  const searchUserListData = computed(() => store.state.searchUserListData)
  
  // 搜索人员的 组织架构下人员数据
  const organizationUserList = computed(() => {
    return searchUserListData.value.organizationUserList
  })
  
  // 搜索人员的 服务商下人员数据
  const serviceProviderUserList = computed(() => {
    return searchUserListData.value.serviceProviderUserList
  })
  
  // 搜索人员的 组织架构下人员数据是否为空
  const isOrganizationUserListEmpty = computed(() => {
    return isEmpty(organizationUserList.value)
  })
  
  // 搜索人员的 服务商下人员数据是否为空
  const isServiceProviderUserListEmpty = computed(() => {
    return isEmpty(serviceProviderUserList.value)
  })
  
  // 搜索人员的 组织架构下人员数据是否不为空
  const isOrganizationUserListNotEmpty = computed(() => {
    return isNotEmpty(organizationUserList.value)
  })
  
  // 搜索人员的 服务商下人员数据是否不为空
  const isServiceProviderUserListNotEmpty = computed(() => {
    return isNotEmpty(serviceProviderUserList.value)
  })
  
  const setSearchUserListData = (value: BaseSelectUserStoreSelectUserListDataType) => {
    store.commit(BaseSelectUserVuexPropertyEnum.SetSearchUserListData, value)
  }
  
  return {
    searchUserListData,
    organizationUserList,
    serviceProviderUserList,
    isOrganizationUserListEmpty,
    isServiceProviderUserListEmpty,
    isOrganizationUserListNotEmpty,
    isServiceProviderUserListNotEmpty,
    setSearchUserListData
  }
}

/** 
 * @description 选人控件 搜索人员的列表数据
*/
function useBaseSelectUserStoreSearchKeyword() {
  
  const store = useBaseSelectUserStore()
  
  const keyword = computed(() => store.state.keyword)
  
  const isKeywordEmpty = computed(() => {
    return isEmpty(keyword.value)
  })
  
  const setKeyword = (value: string) => {
    store.commit(BaseSelectUserVuexPropertyEnum.SetKeyword, value)
  }
  
  return {
    keyword,
    isKeywordEmpty,
    setKeyword
  }
}

/** 
 * @description 选人控件 当前搜索更多人员的类型
*/
function useBaseSelectUserStoreCurrentSearchMoreType() {
  
  const store = useBaseSelectUserStore()
  
  const currentSearchMoreType = computed(() => store.state.currentSearchMoreType)
  
  const isCurrentSearchMoreTypeEmpty = computed(() => {
    return isEmpty(currentSearchMoreType.value)
  })
  
  const isCurrentSearchMoreTypeNotEmpty = computed(() => {
    return isNotEmpty(currentSearchMoreType.value)
  })
  
  const setCurrentSearchMoreType = (value: BaseSelectUserSearchMoreTypeEnum | null = null) => {
    store.commit(BaseSelectUserVuexPropertyEnum.SetCurrentSearchMoreType, value)
  }
  
  return {
    currentSearchMoreType,
    isCurrentSearchMoreTypeEmpty,
    isCurrentSearchMoreTypeNotEmpty,
    setCurrentSearchMoreType
  }
}

/** 
 * @description 选人控件 是否含有组织架构成员更多数据
*/
function useBaseSelectUserStoreIsSearchOrganizationHasMore() {
  
  const store = useBaseSelectUserStore()
  
  const isSearchOrganizationHasMore = computed(() => store.state.isSearchOrganizationHasMore)
  
  const setIsSearchOrganizationHasMore = (value: boolean) => {
    store.commit(BaseSelectUserVuexPropertyEnum.SetIsSearchOrganizationHasMore, value)
  }
  
  return {
    isSearchOrganizationHasMore,
    setIsSearchOrganizationHasMore
  }
}

/** 
 * @description 选人控件 是否含有服务商成员更多数据
*/
function useBaseSelectUserStoreIsSearchSpHasMore() {
  
  const store = useBaseSelectUserStore()
  
  const isSearchSpHasMore = computed(() => store.state.isSearchSpHasMore)
  
  const setIsSearchSpHasMore = (value: boolean) => {
    store.commit(BaseSelectUserVuexPropertyEnum.SetIsSearchSpHasMore, value)
  }
  
  return {
    isSearchSpHasMore,
    setIsSearchSpHasMore
  }
}

/** 
 * @description 查询选人控件 搜索人员列表数据
*/
function useBaseSelectUserStoreFetchSearchUserList() {
  
  const store = useBaseSelectUserStore()
  
  function fetchSearchUserList(params: BaseSelectUserGetSearchUserListModel): Promise<MsgModel<BaseSelectUserGetSearchUserListResult>> {
    return (
      store.dispatch(BaseSelectUserVuexPropertyEnum.FetchSearchUserList, params).then(result => {
        return result
      })
    )
  }
  
  return fetchSearchUserList
}

/** 
 * @description 查询选人控件 搜索更多人员列表数据
*/
function useBaseSelectUserStoreFetchSearchMoreUserList() {
  
  const store = useBaseSelectUserStore()
  
  function fetchSearchMoreUserList(params: BaseSelectUserGetSearchMoreUserListModel): Promise<MsgModel<BaseSelectUserGetSearchMoreUserListResult>> {
    return (
      store.dispatch(BaseSelectUserVuexPropertyEnum.FetchSearchMoreUserList, params).then(result => {
        return result
      })
    )
  }
  
  return fetchSearchMoreUserList
}

export {
  useBaseSelectUserStoreSearchUserListData,
  useBaseSelectUserStoreSearchKeyword,
  useBaseSelectUserStoreFetchSearchUserList,
  useBaseSelectUserStoreFetchSearchMoreUserList,
  useBaseSelectUserStoreCurrentSearchMoreType,
  useBaseSelectUserStoreIsSearchOrganizationHasMore,
  useBaseSelectUserStoreIsSearchSpHasMore
}
